package com.jsmtr.rms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.Ex_Users;
import com.jsmtr.rms.service.IExUsersService;
import com.jsmtr.rms.utils.BeanTools;

@Service
public class ExUsersService implements IExUsersService {

    @Resource
    private DaoService dao;

    public void setDao(DaoService dao) {
        this.dao = dao;
    }

    @Override
    public Ex_Users getExUser(String id) throws Exception {
        return (Ex_Users) dao.findFirst("from Ex_Users where id='" + id + "'");
    }

    @Override
    public Ex_Users getExUserByCode(String code) throws Exception {
        return (Ex_Users) dao.findFirst("from Ex_Users where code='" + code + "'");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Ex_Users> getExUsers(String[] ids) throws Exception {
        return dao.find("from Ex_Users where id in(:idlist)", ids);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getExUsers(int page, int perpage, Ex_Users user) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuffer queryHQL = new StringBuffer("from Ex_Users r where 1=1 ");
        StringBuffer countHQL = new StringBuffer("select count(*) from Ex_Users r where 1=1 ");
        StringBuffer condition = new StringBuffer();

        condition.append(BeanTools.hqlWhere(user, "r"));

        queryHQL.append(condition);
        countHQL.append(condition);

        double count = ((Long) dao.findUnique(countHQL.toString())).doubleValue();
        double pageCount = Math.ceil(count / (double) perpage);

        List<DUserResume> list = dao.findLimit(queryHQL.toString(), perpage * (page - 1), perpage);

        map.put("pageCount", pageCount);
        map.put("count", count);
        map.put("list", list);
        return map;
    }
}
